package fr.istic.linkedcv.client.presentation.views.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.google.gwt.cell.client.TextCell;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.cellview.client.CellList;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.view.client.SelectionChangeEvent;
import com.google.gwt.view.client.SingleSelectionModel;

import fr.istic.linkedcv.client.MessageCli;
import fr.istic.linkedcv.client.presentation.Ui;
import fr.istic.linkedcv.client.presentation.views.View;
import fr.istic.linkedcv.client.services.PersonneService;
import fr.istic.linkedcv.client.services.PersonneServiceAsync;

public class ListeMessages extends View {

	private final PersonneServiceAsync personneServiceAsync = GWT.create(PersonneService.class);
	
	private HorizontalPanel hPanel = new HorizontalPanel();
	private CellList<String> cellList;
	private List<MessageCli> listMessageCli;
	
	public ListeMessages(Ui ui) {
		super(ui);
	}

	@Override
	public void addProperties(Map<String, String> properties) {
		// TODO Auto-generated method stub
		super.addProperties(properties);
	}

	@Override
	public void addProperty(String key, String value) {

		super.addProperty(key, value);
	}

	@Override
	public void build() {
		
//		List<MessageCli> messages = new ArrayList<MessageCli>();
//		MessageCli messageCli = new MessageCli();
//		messageCli.setTitre("test");
//		messages.add(messageCli);
//		createCellList(messages);
		personneServiceAsync.seeMessages(new AsyncCallback<List<MessageCli>>() {

			@Override
			public void onSuccess(List<MessageCli> result) {
				listMessageCli = result;
				createCellList();
			}

			@Override
			public void onFailure(Throwable caught) {
				Window.alert("failure");

			}
		});
		


	}

	protected void createCellList() {
		TextCell textCell = new TextCell();
	
		 cellList = new CellList<String>(textCell);
		 cellList.addStyleName("cellules");
		final SingleSelectionModel<String> selectionModel = new SingleSelectionModel<String>();
		cellList.setSelectionModel(selectionModel);
		selectionModel.addSelectionChangeHandler(new SelectionChangeEvent.Handler() {
			public void onSelectionChange(SelectionChangeEvent event) {
				String selected = selectionModel.getSelectedObject();
				if (selected != null) {
					
					fillPanel(selected,cellList);
				}
			}
		});
		selectionModel.setSelected(listMessageCli.get(0).getTitre(), true);
		
	}

	protected void fillPanel(String selected, CellList<String> cellList) {
		hPanel.clear();
	    cellList.setRowCount(listMessageCli.size(), true);
	    List<String> listMessagesTitle = new ArrayList<String>();
	    VerticalPanel vPanel = new VerticalPanel();
	    for(MessageCli messageCli : listMessageCli){
	    	String titreMessage = messageCli.getTitre();
	    	if(messageCli.getTitre().equals(selected)){
		    
				vPanel.add(new HTML("<h1>"+titreMessage+"</h1>"));
				vPanel.add(new HTML("<div>"+messageCli.getMessage()+"</div>"));
				vPanel.add(new HTML("<div style='font-size:10px;font-style:italic;color:#CCCCCC;'>Ce message a été envoyé par : "+messageCli.getEnvoyeur()));
	    	}	
			listMessagesTitle.add(titreMessage);
	    	
	    }
	    cellList.setRowData(0, listMessagesTitle);
		hPanel.add(cellList);
		hPanel.add(vPanel);
		
	}

	@Override
	public Widget getWidget() {
		// TODO Auto-generated method stub
		return hPanel;
	}

}
